package org.yankun.exam4j.common.daobase;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;


@Repository
public class DaoBase<T> extends HibernateDaoSupport{
	
	
	@Autowired
	public DaoBase(SessionFactory sf) {
		super.setSessionFactory(sf);
	}
	
	public Serializable stringToIdentifier(String src) {
		return Long.valueOf(src);
	}

	public Serializable[] stringToIdentifier(String[] src) {
		if(null == src)
			return null;
		
		Serializable[] seri = new Serializable[src.length];
		for(int i=0;i<src.length;i++){
			seri[i] = stringToIdentifier(src[i]);
		}
		
		return seri;
	}
	
	public Query createSQLQuery(Session session,String sql,Object[] values){
		Query queryObject = session.createSQLQuery(sql);
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				queryObject.setParameter(i, values[i]);
			}
		}
		return queryObject;
	}
	
	public Query createHqlQuery(Session session,String hql,Object[] values){
		Query queryObject = session.createQuery(hql);
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				queryObject.setParameter(i, values[i]);
			}
		}
		return queryObject;
	}
	
	@SuppressWarnings("unchecked")
	public List<T> hql(String hql, Object[] parameters) {
		return super.getHibernateTemplate().find(hql, parameters);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> hql(String hql) {
		
		return getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> hql(String hql, int max){
		return super.getSession().createQuery(hql).setMaxResults(max).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<T> hql(String hql, int max,Object[] values){
		return this.createHqlQuery(super.getSession(), hql, values).setMaxResults(max).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<T> sql(String sql,Object[] values){
		return this.createSQLQuery(super.getSession(), sql, values).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<T> sql(String sql, int max){
		return super.getSession().createSQLQuery(sql).setMaxResults(max).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<T> sql(String sql, int max,Object[] values){
		return this.createSQLQuery(super.getSession(), sql, values).setMaxResults(max).list();
	}
	
	public void clear() {
		getHibernateTemplate().clear();
	}
	
	public Serializable save(T obj){
		return getHibernateTemplate().save(obj);
	}
	
	public void update(T obj){
		getHibernateTemplate().update(obj);
	}
	
	public void delete(T obj){
		getHibernateTemplate().delete(obj);
	}
	
	public void merge(T obj) {
		getHibernateTemplate().merge(obj);
	}

	public void persist(T obj) {
		getHibernateTemplate().persist(obj);
	}

	public void evict(T obj) {
		getHibernateTemplate().evict(obj);
	};
		
	public void flush() {
		getHibernateTemplate().flush();
	}
}